package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.CompetitorProductDataItem;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.DateUtils;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorControlDataItem;
import com.shinbada.modules.competitor.service.CompetitorControlDataItemService;

import com.shinbada.excel.ExcelKit;

/**
 * 控制器-数据项Controller
 * @author initcap
 * @version 2021-08-05
 */
@RestController
@RequestMapping(value = "/competitor/competitorControlDataItem")
@Api(tags = "控制器-数据项")
public class CompetitorControlDataItemController extends BaseController {

	@Autowired
	private CompetitorControlDataItemService competitorControlDataItemService;

	@ModelAttribute
	public CompetitorControlDataItem get(@RequestParam(required=false) String id) {
		CompetitorControlDataItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorControlDataItemService.get(id);
		}
		if (entity == null){
			entity = new CompetitorControlDataItem();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("控制器-数据项列表数据")
	public AjaxJson list(CompetitorControlDataItem competitorControlDataItem, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorControlDataItem> page = competitorControlDataItemService.findPage(new Page<>(request, response), competitorControlDataItem);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取控制器-数据项数据")
	public AjaxJson queryById(CompetitorControlDataItem competitorControlDataItem) {
		return AjaxJson.success().put("competitorControlDataItem", competitorControlDataItem);
	}

	@RequiresPermissions(value={"competitor:competitorControlDataItem:add","competitor:competitorControlDataItem:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存控制器-数据项")
	public AjaxJson save(CompetitorControlDataItem competitorControlDataItem) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorControlDataItem);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorControlDataItemService.save(competitorControlDataItem);
		return AjaxJson.success("保存控制器-数据项成功");
	}

	@RequiresPermissions("competitor:competitorControlDataItem:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除控制器-数据项")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorControlDataItemService.delete(competitorControlDataItemService.get(id));
		}
		return AjaxJson.success("删除控制器-数据项成功");
	}

	@RequiresPermissions("competitor:competitorControlDataItem:export")
	@GetMapping("export")
	@ApiOperation("导出控制器-数据项")
	public void export(CompetitorControlDataItem entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorControlDataItem.class, response).downXlsx(competitorControlDataItemService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorControlDataItem:import")
	@GetMapping("template")
	@ApiOperation("导出控制器-数据项模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorControlDataItem.class, response).downXlsx(new ArrayList<>(), true);
	}

	@RequiresPermissions("competitor:competitorControlDataItem:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorControlDataItem> result = competitorControlDataItemService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}
	@RequiresPermissions("user")
	@GetMapping("getDataListByNameAndType")
	@ApiOperation("控制器根据数据项名称和类型获取对应数据集")
	public AjaxJson getDataListByNameAndType(CompetitorProductDataItem competitorProductDataItem) {
		List<CompetitorControlDataItem> competitorControlDataItemList = competitorControlDataItemService.getDataListByNameAndType(competitorProductDataItem);
		return AjaxJson.success().put("data",competitorControlDataItemList);
	}
}
